﻿@{
    ViewBag.CurrentMenu = "User";
    Layout = "_Layout";
}
<div class="layui-row">
    <div class="layui-col-md3">
        <ul id="UserTree" class="ztree"></ul>
    </div>
    <div class="layui-col-md9">
        <div id="searchBar"></div>
        <table id="userTable" class="layui-table" lay-filter="userTable"></table>
    </div>
</div>
<script type="text/javascript">
    var selectedDepartId = "00000000-0000-0000-0000-000000000000";
    var cols = "";

    layui.use(['table', 'ztree', 'jquery','searchTable'],
        function() {
            var table = layui.table, ztree = layui.ztree, $ = layui.jquery, searchTable = layui.searchTable;

            function loadCols() {
                $.ajax({
                    url: '@Url.Action("GetUserTableCols", "User")',
                    type: 'post',
                    dataType: 'json',
                    success: function(res) {
                        if (res.success) {
                            cols = [res.data];
                        } else {
                            admin_error(res.msg);
                        }
                    }
                });
            }
            var initTree = function () {
                var zTreeObj;
                var setting = {
                    view: { selectedMulti: false },
                    data: {
                        key: {
                            name: 'text',
                            title: 'text'
                        },
                        simpleData: {
                            enable: true,
                            idKey: 'id',
                            pIdKey: 'parent'
                        }
                    },
                    callback: {
                        beforeClick: beforeClick
                    }
                };
                $.ajax({
                    type: "Get",
                    url: "/Department/GetTreeData?_t=" + new Date().getTime(), //获取数据的ajax请求地址
                    success: function (res) {
                        if (!res.success) {
                            admin_error(res.msg);
                        }
                        zTreeObj = $.fn.zTree.init($("#UserTree"), setting, res.data);
                        zTreeObj.expandAll(true);
                        $("#UserTree_1_a").click();
                    }
                });
            };
            function beforeClick(treeId, treeNode, clickFlag) {
                if (treeNode) {
                    selectedDepartId = treeNode.id;
                    loadTables();
                };
                return (treeNode.click != false);
            }
            //加载表格数据，加上window.xx可以在layui.use 外部调用
            var loadTables = function () {
                var tableIns = table.render({
                    elem: '#userTable',
                    id: 'userTable',
                    url: "/User/GetUserByDepartment?departmentId=" + selectedDepartId + "&_t=" + new Date().getTime(),
                    page: true,
                    loading: true,
                    method: 'post',
                    toolbar: '#toolbarButton',
                    height: 'full-120',
                    cols: cols
                });
                searchTable.init(tableIns);
            };
            $(document).ready(function () {
                loadCols();
                initTree();
            });
            //表头工具栏事件
            table.on('toolbar(userTable)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                switch (obj.event) {
                    case 'editUser':
                        if (data.length !== 1) {
                            admin_warning("请选择一行数据");
                            return false;
                        }
                        top.layer.open({
                            title: '编辑',
                            area: ["600px", "500px"],
                            type: 2,
                            maxmin: true,
                            content: '@Url.Action("EditPage", "User")?id=' + data[0].id,
                            end: function () {
                                table.reload("userTable");
                            }
                        });
                        break;
                    case 'addUser':
                        top.layer.open({
                            title: '新增',
                            area: ["600px", "500px"],
                            type: 2,
                            maxmin: true,
                            content: '@Url.Action("EditPage", "User")?departmentId=' + selectedDepartId,
                            end: function () {
                                table.reload("userTable");
                            }
                        });
                        break;
                    case 'disableUser':
                        var ids = '';
                        for (var i = 0; i < data.length; i++) {
                            ids += data[i].id + ',';
                        }
                        if (ids.length < 1) {
                            admin_warning("请选择要禁用的用户");
                            return;
                        }; //询问框
                        layer.confirm("您确认禁用选定的用户吗？",
                            {
                                btn: ["确定", "取消"]
                            },
                            function (index) {
                                layer.close(index);
                                var sendData = { "ids": ids };
                                $.ajax({
                                    url: '@Url.Action("DisableMuti", "User")',
                                    type: 'post',
                                    data: sendData,
                                    dataType: 'json',
                                    success: function (res) {
                                        if (res.success) {
                                            admin_success();
                                            table.reload("userTable");
                                        } else {
                                            admin_error(res.msg);
                                        } 
                                    } 
                                    
                                });
                            });
                        break;
                    case 'ableUser':
                        var ids = '';
                        for (var i = 0; i < data.length; i++) {
                            ids += data[i].id + ',';
                        }
                        if (ids.length < 1) {
                            admin_warning("请选择要启用的用户");
                            return;
                        }; //询问框
                        layer.confirm("您确认启用选定的用户吗？",
                            {
                                btn: ["确定", "取消"]
                            },
                            function (index) {
                                layer.close(index);
                                var sendData = { "ids": ids };
                                $.ajax({
                                    url: '@Url.Action("AbleMuti", "User")',
                                    type: 'post',
                                    data: sendData,
                                    dataType: 'json',
                                    success: function (res) {
                                        if (res.success) {
                                            admin_success();
                                            table.reload("userTable");
                                        } else {
                                            admin_error(res.msg);
                                        }
                                    }
                                });
                            });
                        break;
                }
            });
    });
</script>